package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.dto.CaScheduleLessonDto;
import com.dingreading.cloud.admin.dto.CallMemberDto;
import com.dingreading.cloud.admin.entity.CaScheduleLesson;
import com.dingreading.cloud.admin.entity.table.CaScheduleLessonTableDef;
import com.dingreading.cloud.admin.mapper.CaScheduleLessonMapper;
import com.dingreading.cloud.admin.service.CaScheduleLessonService;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class CaScheduleLessonServiceImpl extends BaseServiceImpl<CaScheduleLessonMapper, CaScheduleLesson, CaScheduleLessonTableDef> implements CaScheduleLessonService {


    @Override
    protected CaScheduleLessonTableDef getTable() {
        return CaScheduleLessonTableDef.caScheduleLesson;
    }

    @Resource
    CaScheduleLessonMapper scheduleLessonMapper;

    @Override
    public Page<CaScheduleLessonDto> pageListV3(
            PageUtil pageUtil, String corpUid, String storeUid, String keyword, Integer status, String scheduleUid, String lessonUid,
            Long classId, String className, Long roomId, String username, String projectUid, String beginDate, String endDate
    ) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(corpUid))
            condition.and(table.corpUid.eq(corpUid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.roomName.like(keyword)
                    .or(table.className.like(keyword))
                    .or(table.productName.like(keyword))
                    .or(table.teacherName.like(keyword))
            );
        if (null != status)
            condition.and(table.status.eq(status));
        if (null != scheduleUid)
            condition.and(table.scheduleUid.eq(scheduleUid));
        if (null != lessonUid)
            condition.and(table.lessonUid.eq(lessonUid));
        if (StringUtils.isNotBlank(className))
            condition.and(table.className.like(className));
        if (null != classId)
            condition.and(table.classId.eq(classId));
        if (null != roomId)
            condition.and(table.roomId.eq(roomId));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        if (StringUtils.isNotBlank(username))
            condition.and(table.usernames.like("," + username + ","));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.teachDate.between(beginDate, endDate));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.teachDate.desc(), table.beginTime.asc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, CaScheduleLessonDto.class);
    }

    @Override
    public CaScheduleLesson getLessonInfo(String storeUid, String lessonUid) {
        QueryCondition condition = table.lessonUid.eq(lessonUid);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<CallMemberDto> getClassMemberAndCallMember(String storeUid, Long classId, String lessonUid) {
        List<CallMemberDto> list = null;
        if (classId != null && classId > 0) {
            list = scheduleLessonMapper.getClassMemberAndCallMemberByClassId(storeUid, classId, lessonUid);
        } else {
            // 快速创建的活动,没有社团id
            list = scheduleLessonMapper.getClassMemberAndCallMember(storeUid, lessonUid);
        }
        return list;
    }

    @Override
    public List<CaScheduleLesson> getStoreLessons(String corpUid, String storeUid, String keyword, Integer status, String beginDate, String endDate, List<String> storeUidList) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(corpUid))
            condition.and(table.corpUid.eq(corpUid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (CollectionUtils.isNotEmpty(storeUidList))
            condition.and(table.storeUid.in(storeUidList));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.productName.like(keyword)
                    .or(table.teacherName.like(keyword))
            );
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.teachDate.between(beginDate, endDate));
        if (null != status)
            condition.and(table.status.eq(status));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.teacherName.asc(), table.teachDate.desc());
        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public boolean haveProjectUid(String projectUid) {
        QueryCondition condition = table.projectUid.eq(projectUid);
        return exists(condition);
    }

    @Override
    public boolean haveStageUid(String storeUid, String stageUid) {
        QueryCondition condition = table.stageUid.eq(stageUid);
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        return exists(condition);
    }


}
